<template>
  <div>
    <div style="display: flex;justify-content: center;margin-top: 10px;">
      <el-input v-model="keywords" placeholder="通过用户名搜索用户..." prefix-icon="el-icon-search"
                style="width: 400px;margin-right: 10px;"></el-input>
      <el-button type="primary" @click="search()">搜索</el-button>
    </div>
    <div class="admin-content">
      <el-card class="admin-card" v-for="(admin,index) in admins" :key="index">
        <div slot="header" class="clearfix">
          <span>{{ admin.name }}</span>
          <el-button @click="deleteAdmin(admin)" style="float: right; padding: 3px 0;color: red;" type="text"
                     icon="el-icon-delete"></el-button>
        </div>
        <div>
          <div class="img-content">
            <img :src="admin.userFace" alt="头像" :title="admin.name" class="userFace-img">
          </div>
        </div>
        <div class="userInfo-content">
          <div>用户名：{{ admin.name }}</div>
          <div>手机号：{{ admin.phone }}</div>
          <div>电话号码：{{ admin.telephone }}</div>
          <div>地址：{{ admin.address }}</div>
          <div>用户状态：
            <el-switch
                @change="enabledChange(admin)"
                style="padding-top: 5px;"
                v-model="admin.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="启用"
                inactive-text="禁用">
            </el-switch>
          </div>
          <div>
            用户角色：
            <el-tag style="margin-right: 10px;margin-bottom: 5px;" type="success" v-for="(role,indexj) in admin.roles"
                    :key="indexj">{{ role.nameZh }}
            </el-tag>
            <el-popover
                @hide="updateRoles(admin)"
                @show="showPop(admin)"
                placement="right"
                title="角色列表"
                width="200"
                trigger="click">
              <el-select v-model="selectedRoles" multiple placeholder="请选择">
                <el-option
                    v-for="(role,index) in roles"
                    :key="index"
                    :label="role.nameZh"
                    :value="role.id">
                </el-option>
              </el-select>
              <el-button slot="reference" type="text" icon="el-icon-more"></el-button>
            </el-popover>
          </div>
          <div>
            备注：{{ admin.remark }}
          </div>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SysAdmin',
  data() {
    return {
      admins: [],
      keywords: "",
      roles: [],
      selectedRoles: []
    }
  },
  methods: {
    updateRoles(admin) {
      let flag = false;//判断是否更新了角色，如果没有更新就不必要去更新数据库
      let roles = [];
      Object.assign(roles, admin.roles)
      if (roles.length != this.selectedRoles.length) {
        flag = true;
      } else {
        for (let i = 0; i < roles.length; i++) {
          let roleId = roles[i].id;
          for (let j = 0; j < this.selectedRoles.length; j++) {
            if (roleId == this.selectedRoles[j]) {
              roles.splice(i, 1);
              i--;
              break;
            }
          }
        }
        if (roles.length != 0) {
          flag = true;
        }
      }

      if (flag) {
        let url = "/system/admin/role?adminId=" + admin.id;
        this.selectedRoles.forEach(rid => {
          url += "&rids=" + rid;
        });
        this.putRequest(url).then(resp => {
          if (resp) {
            // this.selectedRoles = [];
            this.initAdmins();
          }
        });
      }
    },
    showPop(admin) {
      this.initAllRoles();
      let roles = admin.roles;
      this.selectedRoles = [];
      roles.forEach(role => {
        this.selectedRoles.push(role.id);
      });
    },
    initAllRoles() {
      this.getRequest("/system/admin/roles").then(resp => {
        if (resp) {
          this.roles = resp;
        }
      });
    },
    initAdmins() {
      this.getRequest("/system/admin/?keywords=" + this.keywords).then(resp => {
        if (resp) {
          this.admins = resp;
        }
      });
    },
    search() {
      this.initAdmins();
    },
    deleteAdmin(admin) {
      this.$confirm('此操作将永久删除[' + admin.name + ']操作员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest("/system/admin/" + admin.id).then(resp => {
          if (resp) {
            this.initAdmins();
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    enabledChange(admin) {
      this.putRequest("/system/admin/", admin).then(resp => {
        if (resp) {
          this.initAdmins();
        }
      });
    }
  },
  mounted() {
    this.initAdmins();
  }
}
</script>

<style>
.admin-card {
  width: 350px;
  margin-bottom: 20px;
}

.admin-content {
  display: flex;
  margin-top: 10px;
  justify-content: space-around;
  flex-wrap: wrap;
}

.userFace-img {
  width: 72px;
  height: 72px;
  border-radius: 72px;
}

.img-content {
  width: 100%;
  display: flex;
  justify-content: center;
}

.userInfo-content {
  font-size: 12px;
  color: #409eff;
}

.userInfo-content div {
  margin-bottom: 10px;
}
</style>